/**
 * Created by LeungZ on 2017/1/13.
 */
class Add {
    constructor($modalInstance, ctgNotification, $timeout, $window, nodeSrv, $stateParams, group, $q) {
        "ngInject";
        this._nodeSrv = nodeSrv;
        this._modalInstance = $modalInstance;
        this._notification = ctgNotification;
        this.code = $stateParams.kDbCode;
        this.selectList = [];
        this.nonSelectError = true;

        $timeout(function () {
            angular.element('.table-fixed-box').css({
                'max-height': ($window.innerHeight - 360) + 'px'
            });
        }, 0, false);

        if (group) {
            this.name = group.name;
            this.id = group.id;

            $q.all([nodeSrv.getNodeList({page: 1, perPage: 99, code: $stateParams.kDbCode}),
                nodeSrv.getNodeList({page: 1, perPage: 99, code: $stateParams.kDbCode}, {groupCode: this.name})])
                .then(data=> {
                    this.nodeList = data[0].nodes;
                    angular.forEach(data[1].nodes, elem=> {
                        for (var i in this.nodeList) {
                            if (elem.nodeIp == this.nodeList[i].nodeIp) {
                                this.nodeList[i].checked = true;
                                this.selectList.push(this.nodeList[i]);
                                break;
                            }
                        }
                    });
                    this.nonSelectError = false;
                }).catch(()=> {
                    this.nodeList = [];
                    this._notification.error('获取宿主机列表数据有误.');
                })

        } else {
            nodeSrv.getNodeList({page: 1, perPage: 99, code: $stateParams.kDbCode})
                .then(data=> {
                    angular.forEach(data.nodes, (_node, index)=> {
                        _node.nodeStatus = _.findWhere(_node.conditions, {type: 'Ready'}).status == 'True';
                    });
                    this.nodeList = data.nodes;

                    $timeout(function () {
                        if (angular.element('.table-fixed-box>table').height() > ($window.innerHeight - 360)) {
                            angular.element('.table-fixed-box').css({
                                'overflow-y': 'scroll',
                                'margin-right': '-16px'
                            })
                        }
                    }, 0, false);
                }).catch(data=> {
                    this.nodeList = [];
                    this._notification.error('获取宿主机列表数据有误.');
                })
        }
    }

    cancel() {
        this._modalInstance.dismiss('cancel');
    }

    save() {
        if (this.nonSelectError) return;

        var nodes = [];
        angular.forEach(this.selectList, function (elem) {
            nodes.push(elem.nodeIp)
        });

        this.loading = true;

        if (this.id) {
            this._nodeSrv.updateGroup({
                name: this.name,
                id: this.id,
                nodes: nodes
            }).then(data=> {
                this._modalInstance.close();
                this._notification.success('更新宿主机组 ' + this.name + ' 成功.');
            }).catch(data=> {
                this._notification.error('更新宿主机组失败. ' + data.resultMsg);
            }).finally(()=> {
                this.loading = false;
            })
        } else {
            this._nodeSrv.saveGroup({
                name: this.name,
                code: this.code,
                nodes: nodes
            }).then(data=> {
                this._modalInstance.close();
                this._notification.success('保存宿主机组 ' + this.name + ' 成功.');
            }).catch(data=> {
                this._notification.error('保存宿主机组失败. ' + data.resultMsg);
            }).finally(()=> {
                this.loading = false;
            })
        }

    }

    nodeSelect(index) {
        if (this.nodeList[index].checked) {
            this.selectList.splice(this.selectList.indexOf(this.nodeList[index]), 1);
            this.nodeList[index].checked = false;
            if (!this.selectList.length) this.nonSelectError = true;
        } else {
            this.nodeList[index].checked = true;
            this.selectList.push(this.nodeList[index]);
            if (this.nonSelectError) this.nonSelectError = false;
        }
    }

    nodeUnSel(index) {
        this.nodeList[this.nodeList.indexOf(this.selectList[index])].checked = false;
        this.selectList.splice(index, 1);
        if (!this.selectList.length) {
            this.nonSelectError = true;
            this.selListExpend = false;
        }
    }

    expendSelList() {
        this.selListExpend = !this.selListExpend;
    }
}
export default Add